package com.ljh.socrescue.ui.screens

import android.widget.ImageView
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import com.gandiva.neumorphic.LightSource
import com.gandiva.neumorphic.NeuAttrs
import com.gandiva.neumorphic.neu
import com.gandiva.neumorphic.shape.CornerShape
import com.gandiva.neumorphic.shape.Flat
import com.gandiva.neumorphic.shape.Pressed
import com.gandiva.neumorphic.shape.RoundedCorner
import com.ljh.socrescue.R
import com.ljh.socrescue.ui.theme.AppColors
import com.ljh.socrescue.ui.theme.black514
import com.ljh.socrescue.ui.theme.black53
import com.ljh.socrescue.ui.theme.gray8B
import com.ljh.socrescue.ui.theme.grayD3
import com.ljh.socrescue.ui.theme.grayE8
import com.ljh.socrescue.ui.theme.grayEB
import com.ljh.socrescue.ui.theme.green5E
import com.ljh.socrescue.ui.theme.greenCF
import com.ljh.socrescue.ui.theme.whiteEF
import com.ljh.socrescue.ui.theme.whiteFD

/**
 * author：LJH on 2025/7/4 14:19
 *
 */
val defaultWidgetPadding = 16.dp
val defaultElevation = 6.dp
val defaultCornerShape: CornerShape = RoundedCorner(12.dp)

@Composable
fun WelComeScreen(
    modifier: Modifier = Modifier
) {
    val brush = Brush.linearGradient(
        colors = listOf(grayEB, whiteEF)
    )
    var msg by remember {
        mutableStateOf("")
    }
    Column(
        modifier = modifier
            .fillMaxSize()
            .background(brush = brush),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.SpaceBetween
    ) {
        Column(
            modifier = modifier
                .fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Spacer(Modifier.height(88.dp))
            AndroidView(
                factory = { context ->
                    ImageView(context).apply {
                        setImageResource(R.drawable.todo5)
                        setScaleType(ImageView.ScaleType.CENTER_CROP)
                    }
                },
                modifier = Modifier
                    .fillMaxWidth()
//                .offset(y = (-30).dp)
            )
            Spacer(Modifier.height(120.dp))
            Column(
                modifier = modifier
                    .fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
//            modifier = Modifier.offset(y = (-30).dp),
                    text = stringResource(R.string.welcome),
                    color = black514,
                    fontSize = 30.sp,
                    fontWeight = FontWeight.Bold,
                    fontFamily = FontFamily.SansSerif
                )
                Spacer(Modifier.height(8.dp))
                Text(
                    modifier = Modifier
//                .offset(y = (-30).dp)
                        .padding(start = 26.dp),
                    text = stringResource(R.string.todo_list_msg),
                    color = black53,
                    fontSize = 17.sp,
                    fontFamily = FontFamily.SansSerif
                )
                Spacer(Modifier.height(8.dp))
                BasicTextField(
                    value = msg,
                    onValueChange = {
                        msg = it
                    },
                    modifier = Modifier
//                .offset(y = (-30).dp)
                        .width(300.dp)
                        .height(50.dp)
                        .padding(start = 10.dp)
//                .neu(defaultFlatNeuAttrs())
//                .background(MaterialTheme.colorScheme.surface, RoundedCornerShape(12.dp)),
                        .neu(
                            lightShadowColor = AppColors.lightShadow(),
                            darkShadowColor = AppColors.darkShadow(),
                            shadowElevation = 5.dp,
                            lightSource = LightSource.LEFT_BOTTOM,
                            shape = Pressed(RoundedCorner(12.dp)),
                        ),
                    textStyle = TextStyle(
                        fontSize = 15.sp,
                        fontFamily = FontFamily.SansSerif,
                        color = gray8B
                    ),
                    maxLines = 1,
                    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
                    decorationBox = { innerTextField ->
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .padding(horizontal = 16.dp, vertical = 8.dp),
                            contentAlignment = Alignment.CenterStart
                        ) {
                            if (msg.isEmpty()) {
                                Text(
                                    text = stringResource(R.string.enter_your_name),
                                    fontSize = 14.sp,
                                    fontFamily = FontFamily.SansSerif,
                                    color = gray8B
                                )
                            }
                            innerTextField() // ⚠️ 这里一定要调用，否则不会显示输入内容
                        }
                    }
                )
            }
            Spacer(Modifier.height(45.dp))
            Button(
                onClick = {},
                modifier = Modifier
                    .width(200.dp)
                    .height(80.dp)
                    .neu(
                        lightShadowColor = whiteFD,
                        darkShadowColor = AppColors.darkShadow(),
                        shadowElevation = 8.dp,
                        lightSource = LightSource.LEFT_BOTTOM,
                        shape = Pressed(RoundedCorner(36.dp)),
                    ),
                colors = ButtonDefaults.buttonColors(
                        containerColor = greenCF,
                        contentColor = green5E,
                    )
            ) {
                Text(
                    text = stringResource(R.string.continue_btn)
                )
            }
        }

        Spacer(Modifier.height(16.dp))
    }
}

@Composable
fun defaultFlatNeuAttrs() = NeuAttrs(
    lightShadowColor = AppColors.lightShadow(),
    darkShadowColor = AppColors.darkShadow(),
    shadowElevation = defaultElevation,
    lightSource = LightSource.LEFT_TOP,
    shape = Flat(defaultCornerShape)
)
